package com.jml.雪崩和穿透效应.service;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserCacheService {

	@Autowired
	private RedisService redisService;

	private Lock lock = new ReentrantLock();
	public String userLockMethod(Long id){
		//查询redis，没有再查数据库，直接返回
		String key = this.getClass().getName() + "-" + Thread.currentThread().getStackTrace()[1].getMethodName()
				+ "-id:" + id;
		try {
			lock.lock();
			//查询redis   没有 代码忽略这一步
			//查询mysql   有   代码忽略这一步
			//更新redis
			redisService.setObject(key,"jmlredis");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		return key;
	}


	public String userLockMethod1(Long id){
		//查询redis，没有再查数据库，直接返回
		String key = this.getClass().getName() + "-" + Thread.currentThread().getStackTrace()[1].getMethodName()
				+ "-id:" + id;
			//查询redis   没有 代码忽略这一步
			//查询mysql   没有   代码忽略这一步
			//更新redis
			if("数据库中查询的值"==null){
				redisService.setObject(key,"${null}");
			}
		return key;
	}



}
